<template>
	<view class="container">
		<!--index.wxml-->

		<!-- <uni-nav-bar
      left-icon="left"
      :border="false"
      :title="title"
      color="#00000"
      :style="{ marginTop: safeAreaInsets + 'rpx' }"
      fixed="true" /> -->
		<!-- 自定义顶部栏 -->

		<view class="nav-bar" :style="{ height: navBarHeight + 'px' }">
			<view class="search" :style="{
          height: menuHeight + 'px',
          'min-height': menuHeight + 'px',
          'line-height': menuHeight + 'px',
          left: menuRight + 'px',
          top: menuTop + 'px',
        }">
				<uni-icons type="back" size="25" @click="back"></uni-icons>
				<view class="titlebox">{{ title }}</view>
			</view>
		</view>

		<view :style="{ height: navBarHeight + 'px' }"></view>

		<!-- 图标 -->
		<view class="logoimgbox">
			<img src="/static/logo.png" alt="" class="logoimg" />
		</view>
		<!-- 获取手机号使用手机号登录 -->

		<view class="logintips" v-if="sqdlshow">
			<span>
				您暂时未授权小程序名称获取你的信息，将无法正常
				使用小程序的功能。如需要正常使用，请点击“授权”
				按钮，打开头像，昵称等信息的授权。
			</span>
		</view>
		<view v-if="sqdlshow">
			<button type="primary" class="loginbox" @click="kjloginfn">
				微信授权
			</button>
		</view>
		<view class="dllog" v-if="!sqdlshow">
			<view>
				<button type="primary" class="loginbox" @click="sqdlshowfn('1')" v-if="!sqdlshow">
					我是老师
				</button>
			</view>
			<view>
				<button type="primary" class="loginbox" @click="sqdlshowfn('1')" v-if="!sqdlshow">
					我是学生
				</button>
			</view>
			<view>
				<button type="primary" class="loginbox" @click="sqdlshowfn('2')" v-if="!sqdlshow">
					我是访客
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dlbs: 0,
				sqdlshow: false,
				phone: 0,
				kjloginshow: true,
				title: "授权登录",
				safeAreaInsets: null,
				microButtonHeight: null,
				microButtonHeight: null,
				// 数据都是根据当前机型进行计算，这样的方式兼容大部分机器
				navBarHeight: 0, // 导航栏高度
				menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
				menuTop: 0, // 胶囊距底部间距（保持底部间距一致）
				menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
			};
		},

		onLoad() {
			const that = this;
			// 获取系统信息
			const systemInfo = wx.getSystemInfoSync();
			// 胶囊按钮位置信息
			const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
			// 导航栏高度 = 状态栏高度 + 44
			that.navBarHeight = systemInfo.statusBarHeight + 44;
			that.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
			that.menuTop = menuButtonInfo.top;
			that.menuHeight = menuButtonInfo.height;

			// const { statusBarHeight } = uni.getSystemInfoSync();
			// console.log("ss", statusBarHeight);

			//   console.log('sss',uni.getSystemInfoSync());
			//   const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			//   const navBarHeight =
			//     menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2;
			//      const topIconDistance = statusBarHeight + navBarHeight;

			//   console.log('ss',topIconDistance);
			// this.safeAreaInsets = statusBarHeight
			// console.log("ss", this.safeAreaInsets);

			// 如果不是则登录成功 并且记录token 进行跳转到首页
			if (uni.getStorageSync('token')) {
				uni.switchTab({
					url: "/pages/home/home",
					success(res) {
						console.log("跳转成功", res);
					},
					fail(err) {
						console.error("跳转失败", err);
					},
				});
			}

		},
		methods: {
			sqdlshowfn(i) {
				console.log('执行了', i);
				this.dlbs = i
				// 保存一下token
				uni.setStorageSync("dlbs", i);
				this.$nextTick(() => {
					this.sqdlshow = true
				})
			},
			kjloginfn(i) {
				this.dlbs = i
				this.dlshow = true
			},
			back() {
				const pages = getCurrentPages();
				// 如果当前页面栈的长度为1，说明已经是最顶层页面
				if (pages.length === 1) {
					// 跳转到首页
					uni.reLaunch({
						url: "/pages/home/home", // 替换为你的首页路径
					});
				} else {
					// 返回上一页
					uni.navigateBack({
						delta: 1,
					});
				}
			},
			kjloginfn() {
				this.kjloginshow = false;
				// uni.setNavigationBarTitle({
				//   title: "授权登录",
				// });

				//漫路h
				uni.login({
					success: (res) => {

						this.$myRequest({
								url: "/appointment/login",
								method: "POST",
								data: {
									code: res.code,
								},
							})
							.then((res) => {
								console.log("请求成功返回数据", res);
								if (res.message == "账号或密码错误!") {
									uni.navigateTo({
										url: "/pages/safe/safe",
									});
								} else {
									// 保存一下token
									uni.setStorageSync("token", res.data);
									// uni.setStorageSync("openid", res.data); 
									// 如果不是则登录成功 并且记录token 进行跳转到首页
									uni.switchTab({
										url: "/pages/home/home",
										success(res) {
											console.log("跳转成功", res);
										},
										fail(err) {
											console.error("跳转失败", err);
										},
									});
								}
							})
							.catch((err) => {
								console.log("错误", err);
							})
							.finally((res) => {
								console.log("最终", res);
							});




					},
				});
			},
			// 放弃次登录方法
			getPhoneNumber(e) {
				// 修改顶部标题方式
				// uni.setNavigationBarTitle({
				// 				title:'个人中心'
				// 			})
				console.log("获取手机号码", e);
				if (e.detail.errMsg !== "getPhoneNumber:ok") {
					console.log("错误", e.detail.errMsg);
					return;
				}
				uni.showLoading();
				uni.request({
					url: "xxxxxx",
					data: {
						code: e.detail.code,
					},
					method: "GET",
					header: {
						"content-type": "application/json",
					},
					success: (res) => {
						console.log("使用手机号登录", res);
						if (data.statusCode == 200) {
							wx.hideLoading();
							// 实际不给手机号，只给token
							// console.log('获取到的手机号是', res.data)
							uni.setStorageSync("token", res.access_token);
						} else {
							this.$u.toast(res.msg);
						}
					},
					fail: (err) => {
						// this.$u.toast('服务器开小差了呢，请您稍后再试')
						console.log(err);
					},
				});
			},
		},
	};
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
		display: flex;
		flex-direction: column;
		/* 	align-items: center; */
		justify-content: center;
	}

	.loginbox {
		width: 567rpx;
		height: 85rpx;
		background: #04cb64 !important;
		border-radius: 18rpx 18rpx 18rpx 18rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 36rpx;
		/* 	position: absolute;
		top: 609rpx;
		left: 50%;
		transform: translate(-50%,0); */
	}

	.loginboxs {
		background: #1371ff !important;
	}

	.loginbox img {
		width: 38rpx;
		height: 38rpx;
		margin: 0rpx 10rpx;
		display: block;
	}

	.logoimgbox {
		display: flex;

		align-items: center;
		justify-content: center;
	}

	.logoimg {
		width: 408rpx;
		height: 99rpx;
		display: block !important;
		margin-top: 209rpx;
		/* 	position: absolute;
		top: 209rpx;
		left: 50%;
		transform: translate(-50%,0); */
	}

	.logintips {
		font-size: 24rpx;
		color: #000000;
		width: 560rpx;

		margin: 0 auto;
		margin-top: 234rpx;
		line-height: 35rpx;
	}

	.logintips span {
		color: #81818f;
	}

	.uni-nav-bar-text {
		font-size: 32rpx !important;
	}

	/* .uni-tabbar .uni-tabbar__icon {
	width: 90rpx !important;
    height: 90rpx !important;
} */
	.uni-icons .uniui-left {
		font-size: 32rpx;
	}

	.nav-bar {
		position: fixed;
		width: 100%;
		top: 0;
		color: #fff;
		background: #fff;
		display: flex;
	}

	.nav-bar .search {
		color: black;
		font-size: 14px;
		background: #fff;
		position: absolute;
		width: 630rpx;
		/* padding-left: 14px; */
	}

	.titlebox {
		text-align: center;
		position: absolute;
		top: 0;
		font-size: 32rpx;
		width: 200rpx;
		left: 50%;
		transform: translate(-50%, 0);
	}

	.dllog {
		margin-top: 200rpx;
	}
</style>